package logic

import (
	"context"
	"doctor-package/models/mysql"
	"doctor-package/packagerpc/internal/svc"
	"doctor-package/packagerpc/packagerpc"
	"fmt"

	"github.com/zeromicro/go-zero/core/logx"
)

type RegisterUserLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegisterUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterUserLogic {
	return &RegisterUserLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RegisterUserLogic) RegisterUser(in *packagerpc.RegisterUserRequest) (*packagerpc.RegisterUserResponse, error) {
	// todo: add your logic here and delete this line

	var userFin mysql.User
	mysql.DB.Where("account = ?", in.Account).Find(&userFin)
	if userFin.ID != 0 {
		return nil, fmt.Errorf("用户已存在不允许再被注册,请前往登录")
	}

	var user mysql.User
	user = mysql.User{
		Account:  in.Account,
		Password: in.Password,
	}
	err := mysql.DB.Debug().Create(&user).Error
	if err != nil {
		return nil, fmt.Errorf("注册失败")
	}
	return &packagerpc.RegisterUserResponse{Id: int64(user.ID)}, nil
}
